 package kliment.effect {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import kliment.controls.ControlsAbstract;
	import kliment.controls.Orientation;
	import kliment.math.Calc;
	import kliment.utils.Transformation;
	
	/**
	 * ...
	 * @author	Kliment
	 * @version 1.1.3
	 * 
	 * class name: kliment.effect.ZPanoram
	 */
	public class ZPanoram extends ControlsAbstract {
		private var _space_mc:DisplayObject;
		private var _elements:Array;
		private var _ratio:Number;
		private var _orientation:*;
		private var _enableHorizontal:Boolean;
		private var _enableVertical:Boolean;
		private var _pointMouse:Point;
		private var _rectangleBounds:Rectangle;
		private var _mouseInSpace:Boolean = false;
		private var _positionsArray:Array;
		private var _parent:DisplayObject;
		
		public function ZPanoram(space_mc:DisplayObject, elements:Array, ratio:Number = .2, orientation:String = null) {
			_space_mc = space_mc;
			_elements = elements || new Array();
			_ratio = ratio;
			_orientation = orientation || Orientation.FULL;
			_init();
		}
		
		private function _init():void {
			_setOrientation();
			_pointMouse = new Point();
			_positionsArray = new Array();
			enable = true;
		}
		
		override public function set enable(value:Boolean):void {
			_space_mc.removeEventListener(Event.ENTER_FRAME, _moveHandler);
			if (value)
				_space_mc.addEventListener(Event.ENTER_FRAME, _moveHandler);
			super.enable = value;
		}
		
		private function _moveHandler(event:Event = null):void {
			_parent = _space_mc.parent;
			if (!_parent) return;
			_rectangleBounds = _space_mc.getBounds(_parent);
			_pointMouse.x = _parent.mouseX;
			_pointMouse.y = _parent.mouseY;
			_mouseInSpace = _rectangleBounds.containsPoint(_pointMouse);
			var i:Number = _elements.length;
			var item:DisplayObject;
			var pos:Point;
			while (i--) {
				item = _elements[i];	
				pos = _positionsArray[i];
				if (!pos) {
					pos = new Point();
					_positionsArray[i] = pos;
				}
				
				if (_mouseInSpace) {
					if (_enableHorizontal)
						pos.x = -((item.parent.mouseX - _rectangleBounds.x) / _rectangleBounds.width * (item.width - _rectangleBounds.width)) + _rectangleBounds.x;
					if (_enableVertical)
						pos.y = -((item.parent.mouseY - _rectangleBounds.y) / _rectangleBounds.height * (item.height- _rectangleBounds.height)) + _rectangleBounds.y;
				}
				
				var testPoint:Point = Transformation.fixInSpace(new Rectangle(pos.x || item.x, pos.y || item.y, item.width, item.height), _rectangleBounds);			
				
				if (_enableHorizontal) {
					/*if (pos.x != testPoint.x) {
						pos.x = testPoint.x;
						item.x = pos.x;
					} else {*/
						item.x = Calc.deltaRatioResult(pos.x, item.x, _ratio);
					//}
				}
				if (_enableVertical) {
					/*if (pos.y != testPoint.y) {
						pos.y = testPoint.y;
						item.y = pos.y;
					} else {*/
						item.y = Calc.deltaRatioResult(pos.y, item.y, _ratio);
					//}
				}
			}
		}
		
		public function get orientation():Object { return _orientation }
		public function set orientation(value:Object):void {
			_orientation = value;
			_setOrientation();
		}
		
		private function _setOrientation():void {
			if (_orientation == Orientation.HORIZONTAL || _orientation == Orientation.FULL)
				_enableHorizontal = true;
			if (_orientation == Orientation.VERTICAL || _orientation == Orientation.FULL)
				_enableVertical = true;
		}
		
		public function remove():void {
			enable = false;
			_elements = null;
		}
	}

}